/* =============================================================================
 * cohort-specific experience profiles
 * ===========================================================================*/
 
***option for flat spot region
global flat_begin = 50
global flat_end = 59

/*******************************************************************************
		
									U.S.
		
*******************************************************************************/

use "data\US.dta", clear

drop if outlier

keep if male == 1


/*******************************************************************************
construct price series
*******************************************************************************/
preserve

*** flat region
keep if inrange(age,$flat_begin , $flat_end )
collapse (median) mlnearnings = lnearnings (mean) alnearnings = lnearnings [aw = asecwt], by(year age)

reshape wide mlnearnings alnearnings, i(age) j(year)

sort age 

foreach v in m a {

forvalues y = 1987/2012 {
	
	local ylag = `y' - 1
	gen diff_`v'lnearnings`y' = `v'lnearnings`y' - `v'lnearnings`ylag'[_n-1]
	
}
}

collapse diff*

gen i = 1
reshape long diff_mlnearnings diff_alnearnings, i(i) j(year)
drop i

gen accum_lnpm = sum(diff_mlnearnings)
gen accum_lnpa = sum(diff_alnearnings)

keep year accum* 
set obs `=_N+1'
replace year = 1986 if year == .
sort year
replace accum_lnpm = 0 if year == 1986
replace accum_lnpa = 0 if year == 1986

save "temp\price_us.dta", replace

restore


/*******************************************************************************
compute heterogenous experience effects
*******************************************************************************/
merge m:1 year using "temp\price_us.dta"

gen mlnearnings = lnearnings - accum_lnpm
gen alnearnings = lnearnings - accum_lnpa

gen cohort = 10*floor(ybirth/10)

collapse (mean) alnearnings, by (cohort age)

* normalize the profile and specify 1
sum alnearnings if cohort == 1960 & inrange(age,20,25)
gen h = exp(alnearnings - r(mean))

* delete end points that lead to small sample of the estimated cohort-specific experience
keep if inrange(age,20,59)
bys cohort: egen agemax = max(age)
gen tag = 1 if agemax - age < 3 & agemax != 59
drop if tag == 1

twoway (scatter h age if cohort == 1940, msymbol(+) connect(l) color(blue*0.2))		///
	(scatter h age if cohort == 1950, msymbol(th) connect(l) color(blue*0.5))		///
	(scatter h age if cohort == 1960, msymbol(x) connect(l) color(blue*0.75))		///
	(scatter h age if cohort == 1970, msymbol(oh) connect(l) color(blue*1))			///
	(scatter h age if cohort == 1980, msymbol(dh) connect(l) color(blue*1.5)),		///
	xlabel(20(5)59, labsize(medium)) ylabel(1(1)4, labsize(medium)) yscale(range(0.6 4))	 ///
	xtitle("Age") ytitle("") 		///
	legend(order(1 "1940s" 2 "1950s" 3 "1960s" 4 "1970s" 5 "1980s") 	///
	rows(1) pos(6) size(medium)) 		///
	xsize(4) name(us, replace)
graph export "figures\us_cohort_specific_experience.pdf", as(pdf) replace


/*******************************************************************************
		
								China
		
*******************************************************************************/

use "data\China.dta", clear

drop if outlier
keep if male == 1
gen asecwt = 1	

/*******************************************************************************
construct price series
*******************************************************************************/
preserve

*** flat region
keep if inrange(age,$flat_begin , $flat_end )
collapse (median) mlnearnings = lnearnings (mean) alnearnings = lnearnings [aw = asecwt], by(year age)

reshape wide mlnearnings alnearnings, i(age) j(year)

sort age 

foreach v in m a {

forvalues y = 1987/2012 {
	
	local ylag = `y' - 1
	gen diff_`v'lnearnings`y' = `v'lnearnings`y' - `v'lnearnings`ylag'[_n-1]
	
}
}

collapse diff*

gen i = 1
reshape long diff_mlnearnings diff_alnearnings, i(i) j(year)
drop i


gen accum_lnpm = sum(diff_mlnearnings)
gen accum_lnpa = sum(diff_alnearnings)

keep year accum* 
set obs `=_N+1'
replace year = 1986 if year == .
sort year
replace accum_lnpm = 0 if year == 1986
replace accum_lnpa = 0 if year == 1986

save "temp\price_cn.dta", replace

restore


/*******************************************************************************
compute heterogenous experience effects
*******************************************************************************/

merge m:1 year using "temp\price_cn.dta"

gen mlnearnings = lnearnings - accum_lnpm
gen alnearnings = lnearnings - accum_lnpa

gen cohort = 10*floor(ybirth/10)

collapse (mean) alnearnings, by (cohort age)

sum alnearnings if cohort == 1960 & inrange(age,20,25)
gen h = exp(alnearnings - r(mean))

keep if inrange(age,20,59)

bys cohort: egen agemax = max(age)
gen tag = 1 if agemax - age < 3 & agemax != 59
drop if tag == 1

twoway (scatter h age if cohort == 1940, msymbol(+) connect(l) color(red*0.2))		///
	(scatter h age if cohort == 1950, msymbol(th) connect(l) color(red*0.5))		///
	(scatter h age if cohort == 1960, msymbol(x) connect(l) color(red*0.75))		///
	(scatter h age if cohort == 1970, msymbol(oh) connect(l) color(red*1))			///
	(scatter h age if cohort == 1980, msymbol(dh) connect(l) color(red*1.5)),		///
	xlabel(20(5)59, labsize(medium)) ylabel(1(1)4, labsize(medium)) yscale(range(0.6 4)) 	///
	xtitle("Age") ytitle("")	///
	legend(order(1 "1940s" 2 "1950s" 3 "1960s" 4 "1970s" 5 "1980s") 	///
	rows(1) pos(6) size(medium)) 		///
	xsize(4) name(cn, replace)
graph export "figures\cn_cohort_specific_experience.pdf", as(pdf) replace
